package com.wuge.gulimall.product.service.impl;

import com.wuge.gulimall.product.entity.SkuImagesEntity;
import com.wuge.gulimall.product.entity.SpuInfoDescEntity;
import com.wuge.gulimall.product.service.*;
import com.wuge.gulimall.product.vo.SkuItemSaleAttrVo;
import com.wuge.gulimall.product.vo.SkuItemVo;
import com.wuge.gulimall.product.vo.SpuItemAttrGroupVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.product.dao.SkuInfoDao;
import com.wuge.gulimall.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
 @Resource
    private SpuInfoDescService spuInfoDescService;
@Resource
private SkuImagesService skuImagesService;
@Resource
private AttrGroupService attrGroupService;
@Resource
private AttrService attrService;
@Resource
private ThreadPoolExecutor threadPoolExecutor;
//    key: '华为',//检索关键字
//    catelogId: 0,
//    brandId: 0,
//    min: 0,
//    max: 0
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper=new QueryWrapper<>();
        String key = (String)params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.like("sku_name",key);
        }
        String catelogId = (String)params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)){
            if (!"0".equals(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);}
        }
        String brandId = (String)params.get("brandId");
        if (!StringUtils.isEmpty(brandId)){
            if (!"0".equals(brandId)){
                queryWrapper.eq("brand_id",brandId);}
        }
        String min = (String)params.get("min");
        if (!StringUtils.isEmpty(min)){
            queryWrapper.ge("price",min);
        }
        String max = (String)params.get("max");
        if (!StringUtils.isEmpty(max)&&!new BigDecimal(max).equals(BigDecimal.ZERO)){
            queryWrapper.le("price",max);
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> findBySpuId(Long spuId) {
        return list(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
    }

    @Override
    public SkuItemVo queryItemSku(Long skuId) {
        SkuItemVo skuItemVo=new SkuItemVo();
//        异步任务
        //        设置sku的基本信息
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity byId = getById(skuId);
            skuItemVo.setInfo(byId);
            return byId;
        }, threadPoolExecutor);

//        设置spu的描述信息
        CompletableFuture<Void> objectCompletableFuture = skuInfoFuture.thenAcceptAsync((spuId) -> {
            SpuInfoDescEntity descEntity = spuInfoDescService.getById(spuId.getSpuId());
            skuItemVo.setDesc(descEntity);
        });

        //设置sku的图片信息
        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesEntities = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));

            skuItemVo.setImages(imagesEntities);
        }, threadPoolExecutor);


        //设置spu的分组信息
        CompletableFuture<Void> GroupFuture = skuInfoFuture.thenAcceptAsync((spuId) -> {
            Long catalogId = spuId.getCatalogId();
            List<SpuItemAttrGroupVo> groupAttrs= attrGroupService.getSpuAttr(spuId.getSpuId(),catalogId);

            skuItemVo.setGroupAttrs(groupAttrs);
        });

          //设置销售属性
        CompletableFuture<Void> SaleFuture = skuInfoFuture.thenAcceptAsync((spuId) -> {
            List<SkuItemSaleAttrVo> list1=attrService.getAttrs(spuId.getSpuId());
            skuItemVo.setSaleAttr(list1);
        });
        try {
            CompletableFuture.allOf(skuInfoFuture,objectCompletableFuture,imgFuture,GroupFuture,SaleFuture).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return skuItemVo;
    }
}